package org.stvd.controller;

import java.beans.PropertyEditorSupport;
import java.util.regex.Pattern;

public class DataBinderSupport {

    // 字符串，如果不为空，则自动trim空格，为空返回空字符串；
    public static final PropertyEditorSupport StringEditor = new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            Object obj = getValue();
            if (obj != null) {
                return obj.toString().trim();
            } else {
                return "";
            }
        }

        @Override
        public void setAsText(String text) {
            if (text != null) {
                setValue(text.trim());
            } else {
                setValue("");
            }
        }
    };

    // 整型，如果格式不正确，则自动转为0；
    public static final PropertyEditorSupport IntegerEditor = new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            Object obj = getValue();
            if (obj != null) {
                return obj.toString();
            } else {
                return "";
            }
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            // 表示整数
            String intReg = "^-?[0-9]\\d*$";
            Pattern intPattern = Pattern.compile(intReg);
            if (!intPattern.matcher(text).matches()) {
                // setValue(0);
                setValue(null);
            } else {
                setValue(Integer.parseInt(text));
            }
        }
    };

    // 长整型，如果格式不正确，则自动转为0；
    public static final PropertyEditorSupport LongEditor = new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            Object obj = getValue();
            if (obj != null) {
                return obj.toString();
            } else {
                return "";
            }
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            // 表示整数
            String longReg = "^-?[0-9]\\d*$";
            Pattern longPattern = Pattern.compile(longReg);
            if (!longPattern.matcher(text).matches()) {
                // setValue(0);
                setValue(null);
            } else {
                setValue(Long.parseLong(text));
            }
        }
    };

    // 浮点型，如果格式不正确，则自动转为0；
    public static final PropertyEditorSupport DoubleEditor = new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            Object obj = getValue();
            if (obj != null) {
                return obj.toString();
            } else {
                return "";
            }
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            // 表示浮点数
            String doubleReg = "^(-?\\d+)(\\.\\d+)?$";
            Pattern doublePattern = Pattern.compile(doubleReg);
            if (!doublePattern.matcher(text).matches()) {
                // setValue(0d);
                setValue(null);
            } else {
                setValue(Double.parseDouble(text));
            }
        }
    };
}
